package com.owl.app;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;

public class MainSite extends Activity {
	
	private WebView mainWebView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    // TODO Auto-generated method stub
	    setContentView(R.layout.main);
	    
	    // top.xml //////////////////////////////////////////////////////////////////////////
	    top top = new top(this);
	    top.init();
	    
	    /*TextView topTitle = (TextView)findViewById(R.id.topTitle);
	    topTitle.setText(R.string.main_name);
	    
	    ImageButton top_arrow_left = (ImageButton)findViewById(R.id.top_arrow_left);
	    
	    top_arrow_left.setOnClickListener(new ImageButton.OnClickListener(){
	    		public void onClick(View v){
	    			onBackPressed();
	    		}
	    });
	    
	    ImageButton top_arrow_right = (ImageButton)findViewById(R.id.top_arrow_right);
	    top_arrow_right.setVisibility(View.INVISIBLE);*/
	    /*top_arrow_right.setOnClickListener(new ImageButton.OnClickListener(){
	    		public void onClick(View v){
	    			
	    		}
	    });*/
	    
	    mainTabListenerOn();
	    //mainContentToggle(findViewById(R.id.mainMenu1));
	    
	    Intent intent = this.getIntent();
	    String tabId = intent.getStringExtra("tabId");
	    View tmpView = findViewById(R.id.mainMenu1);
	    if("tab2".equals(tabId)){
	    	tmpView = findViewById(R.id.mainMenu2);
	    }else if("tab3".equals(tabId)){
	    	tmpView = findViewById(R.id.mainMenu3);
	    }
	    mainContentToggle(tmpView);
	    
	   /* mainWebView = (WebView)findViewById(R.id.mainwebview);
	    mainWebView.getSettings().setJavaScriptEnabled(true);
	    mainWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
	    mainWebView.getSettings().setPluginsEnabled(true);
	    mainWebView.getSettings().setSupportMultipleWindows(true);
	    mainWebView.getSettings().setSupportZoom(true);
	    mainWebView.getSettings().setBuiltInZoomControls(true);
	    mainWebView.getSettings().setBlockNetworkImage(false);
	    mainWebView.getSettings().setLoadsImagesAutomatically(true);
	    mainWebView.getSettings().setUseWideViewPort(true);
	    mainWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
	    mainWebView.setWebViewClient(new owlWebViewClient());
	    mainWebView.loadUrl("http://www.owllab.com/android/index.html");*/
	    /*mainWebView.setInitialScale(30);*/
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu){
    	
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.option_menu, menu);
    	
    	return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	
    	boolean tmp = new OptionMenu(this).initSelected(item);
    	if(tmp) return tmp;
    	else return super.onOptionsItemSelected(item);
    }
    
	public void loadInfoHTML(){
		
		mainWebView = (WebView)findViewById(R.id.mainwebview);
		mainWebView.getSettings().setJavaScriptEnabled(true);
		mainWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		mainWebView.getSettings().setPluginsEnabled(true);
		mainWebView.getSettings().setSupportMultipleWindows(true);
		mainWebView.getSettings().setSupportZoom(true);
		mainWebView.getSettings().setBuiltInZoomControls(true);
		mainWebView.getSettings().setBlockNetworkImage(false);
		mainWebView.getSettings().setLoadsImagesAutomatically(true);
		mainWebView.getSettings().setUseWideViewPort(true);
		mainWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		mainWebView.setWebViewClient(new owlWebViewClient());
		
		String theUrl = "http://www.owllab.com/android/index.php?company_name_en=OWLLAB&company_name_ko=아울컴";
		
		ArrayList<NameValuePair> httpParams = new ArrayList<NameValuePair>();
		httpParams.add(new BasicNameValuePair("company_name_en", "OWL"));
		httpParams.add(new BasicNameValuePair("company_name_ko", "아울"));
		Log.i("WEB", theUrl);
				
		CmsHTTP cmsHttp = new CmsHTTP();
		cmsHttp.webview = mainWebView;
		cmsHttp.encoding = "UTF-8";
		cmsHttp.act = this;
		cmsHttp.getPost(theUrl, httpParams);
	}
	
	public void mainContentToggle(View v){
		findViewById(R.id.main_tab1).setVisibility(View.INVISIBLE);
		findViewById(R.id.main_tab2).setVisibility(View.INVISIBLE);
		findViewById(R.id.main_tab3).setVisibility(View.INVISIBLE);
		
		if(v.getId() == R.id.mainMenu2){
			findViewById(R.id.main_tab2).setVisibility(View.VISIBLE);
			loadInfoHTML();
		}else if(v.getId() == R.id.mainMenu3){
			findViewById(R.id.main_tab3).setVisibility(View.VISIBLE);
			Category categoryTmp = new Category(this);
			categoryTmp.getXMLDataList();
		}else{
			findViewById(R.id.main_tab1).setVisibility(View.VISIBLE);
			new Notice(this).getXMLDataList();
		}
		
		findViewById(R.id.mainMenu1).setBackgroundResource(R.drawable.main_menu1);
		findViewById(R.id.mainMenu2).setBackgroundResource(R.drawable.main_menu2);
		findViewById(R.id.mainMenu3).setBackgroundResource(R.drawable.main_menu3);
		 
		if(v.getId() == R.id.mainMenu2){
			v.setBackgroundResource(R.drawable.main_menu2_on);
		}else if(v.getId() == R.id.mainMenu3){
			v.setBackgroundResource(R.drawable.main_menu3_on);
		}else{
			v.setBackgroundResource(R.drawable.main_menu1_on);
		}
	}
	public void mainTabListenerOn(){
		findViewById(R.id.mainMenu1).setOnClickListener(new ImageButton.OnClickListener(){
			public void onClick(View v){
				mainContentToggle(v);
			}
		});
		findViewById(R.id.mainMenu2).setOnClickListener(new ImageButton.OnClickListener(){
			public void onClick(View v){
				mainContentToggle(v);
			}
		});
		findViewById(R.id.mainMenu3).setOnClickListener(new ImageButton.OnClickListener(){
			public void onClick(View v){
				mainContentToggle(v);
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
		if((keyCode == KeyEvent.KEYCODE_BACK) && mainWebView.canGoBack()){
			mainWebView.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private class owlWebViewClient extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url){
			view.loadUrl(url);
			return true;
		}
	}
}
